package top.saplf.sileny.editor.home

import android.content.Intent
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.jakewharton.rxbinding2.support.v4.widget.RxSwipeRefreshLayout
import com.jakewharton.rxbinding2.support.v4.widget.refreshes
import io.realm.Realm
import io.realm.Sort
import kotlinx.android.synthetic.main.fragment_editor_index.*
import top.saplf.sileny.R
import top.saplf.sileny.base.ui.SilenyFragment
import top.saplf.sileny.editor.edit.EditorActivity
import top.saplf.sileny.editor.edit.EditorActivity.Companion.noteId

/**
 * @author saplf
 */
class EditorIndexFragment : SilenyFragment() {

    companion object {
        fun getInstance() = EditorIndexFragment().apply {
            // todo
        }

        private const val ADD_REQUEST = 1
        private const val MODIFY_REQUEST = 2
    }

    private val mAdapter: EditorIndexAdapter by lazy {
        EditorIndexAdapter().apply {
            bindToRecyclerView(noteList)
            setEmptyView(R.layout.view_empty)
            addData(mRealm.where(Note::class.java).findAll().sort(Note::modifyTime.name, Sort.DESCENDING))
            itemAction = {
                activity.startActivityForResult(Intent(context, EditorActivity::class.java).apply {
                    noteId = it.id
                }, MODIFY_REQUEST)
            }
            longItemAction = { (id) ->
                AlertDialog.Builder(context)
                        .setMessage("是否删除？")
                        .setPositiveButton("确定") { dialog, _ ->
                            dialog.dismiss()
                            mRealm.beginTransaction()
                            mAdapter.data.find { it.id == id }?.deleteFromRealm()
                            mRealm.commitTransaction()
                            mAdapter.setNewData(mRealm.where(Note::class.java).findAll().sort(Note::modifyTime.name, Sort.DESCENDING))
                        }
                        .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                        .create()
                        .show()
            }
        }
    }
    private val mRealm: Realm by lazy { Realm.getDefaultInstance() }

    @LayoutRes override fun onLayoutRes(): Int = R.layout.fragment_editor_index

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        with(noteList) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }

        swipeLayout.refreshes()
                .subscribe {
                    swipeLayout.isRefreshing = false
                    mAdapter.setNewData(mRealm.where(Note::class.java).findAll().sort(Note::modifyTime.name, Sort.DESCENDING))
                }
    }

    override fun onDestroy() {
        super.onDestroy()
        mRealm.close()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data == null) {
            return
        }
        mAdapter.setNewData(mRealm.where(Note::class.java).findAll().sort(Note::modifyTime.name, Sort.DESCENDING))
    }

    fun actionClick() {
        activity.startActivityForResult(Intent(context, EditorActivity::class.java), ADD_REQUEST)
    }

}